
import delim using "${orig_data}2021_DE_Region_Mobility_Report.csv", clear 

*https://support.google.com/covid19-mobility/answer/9825414?hl=en&ref_topic=9822927

rename retail_and_recreation_percent_ch 	mob_retail_and_recreation
rename grocery_and_pharmacy_percent_cha 	mob_grocery_and_pharmacy
rename parks_percent_change_from_baseli 	mob_parks
rename transit_stations_percent_change_ 	mob_transit_stations 
rename workplaces_percent_change_from_b 	mob_workplaces 
rename residential_percent_change_from_ 	mob_residential



*** Date ***

gen d = substr(date,9,2)
gen m = substr(date,6,2)
gen y = substr(date,1,4)

destring m d y, replace

drop date

gen date = mdy(m,d,y)
format date %td

drop m d y

gen dow = dow(date)


*** BL ***

gen bl 		=  .

replace bl 	=  1 if iso_3166_2_code=="DE-SH"
replace bl 	=  2 if iso_3166_2_code=="DE-HH"
replace bl 	=  3 if iso_3166_2_code=="DE-NI"
replace bl 	=  4 if iso_3166_2_code=="DE-HB"
replace bl 	=  5 if iso_3166_2_code=="DE-NW"
replace bl 	=  6 if iso_3166_2_code=="DE-HE"
replace bl 	=  7 if iso_3166_2_code=="DE-RP"
replace bl 	=  8 if iso_3166_2_code=="DE-BW"
replace bl 	=  9 if iso_3166_2_code=="DE-BY"
replace bl 	= 10 if iso_3166_2_code=="DE-SL"
replace bl 	= 11 if iso_3166_2_code=="DE-BE"
replace bl 	= 12 if iso_3166_2_code=="DE-BB"
replace bl 	= 13 if iso_3166_2_code=="DE-MV"
replace bl 	= 14 if iso_3166_2_code=="DE-SN"
replace bl 	= 15 if iso_3166_2_code=="DE-ST"
replace bl 	= 16 if iso_3166_2_code=="DE-TH"




****** erster Schultag 2021
gen firstday 	= .

replace firstday = td(02Aug2021) if inlist(bl, 13, 1) // MV, SH
replace firstday = td(05Aug2021) if bl == 2 // HH
replace firstday = td(09Aug2021) if bl == 11 //  BE
replace firstday = td(09Aug2021) if bl == 12 //  BB
replace firstday = td(18Aug2021) if bl == 5 //NW
replace firstday = td(30Aug2021) if inlist(bl, 6, 7,10) // HE, RP, SL
replace firstday = td(02Sep2021) if inlist(bl, 3, 4, 15) // NI, HB, ST
replace firstday = td(06Sep2021) if inlist(bl, 14, 16) // SN, TH
replace firstday = td(13Sep2021) if bl == 9 // BY
replace firstday = td(14Sep2021) if bl == 8 // BW
	
gen daystofirst  = date - firstday


	
format first* %td

recode daysto (min / -15 = -15) (42 / max = 42) 

qui tab daysto, gen(D_)		

drop D_16 

*** Sample ***

gen idbundesland = bl

save "${altered_data}google.dta", replace

keep if date >= td(27jul2021) & date <= td(04oct2021)

* postfile
postutil clear		
postfile Googleestim str244(outcome) double(base day beta cil cih se p obs r2_w) using "${altered_data}Google_estim.dta" , replace	

	foreach trip of varlist mob_* {		
		

			reghdfe `trip' D_*, a(bl##dow date) cluster(bl)		
			
			local N 	= e(N)
			local r2_w 	= e(r2_within)
			
			forval d = 1/59 {	
			
				local day = `d' - 16
			
				if `d'==16 {		
					local base  = 1
					local b  	= 0 
					local cil 	= 0
					local cih 	= 0	
					local se 	= 0
					local p 	= 0	
				}
				else {
					cap di _b[D_`d']			
					if _rc==0 {
						local base  = 0
						local b   	= _b[D_`d'] 
						local cil 	= _b[D_`d'] - 1.96 * _se[D_`d']
						local cih 	= _b[D_`d'] + 1.96 * _se[D_`d']	
						local se 	= _se[D_`d']
						local p 	= (2 * ttail(e(df_r), abs(_b[D_`d']/_se[D_`d'])))
					}
					else {
						local base  = 0
						local b  	= . 
						local cil 	= .
						local cih 	= .	
						local se 	= .
						local p 	= .
					}
				}
			
				post Googleestim ("`trip'") (`base') (`day') (`b') (`cil') (`cih') (`se') (`p') (`N') (`r2_w')
			
			} 
		
	} 



* post close
postclose Googleestim

